package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.common.RedisConst;
import com.itheima.health.entity.Result;
import com.itheima.health.service.OrderService;
import com.itheima.health.utils.Lock;
import com.itheima.health.utils.ZookeeperLock;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Map;

/**
 * @Author likepei
 * @Date 2020/4/6 23:33
 * @Version v1.0
 * @Description 体检预约业务控制类
 */
@RestController
@Slf4j
@RequestMapping("/mobile/order")
public class OrderController {

    //依赖注入
    @Autowired
    private JedisPool jedisPool;

    @Reference
    private OrderService orderService;

    /**
     * 基于预约订单id 查询体检预约成功的信息
     * @param id 预约订单id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            //调用service , 获取体检预约成功的信息
            Result result = orderService.findById4Detail(id);
            //获取数据成功, 返回成功信息
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            //获取数据失败, 返回失败信息
            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }


    /**
     * 提交体检预约
     * 1. 验证短信验证码
     *    1. 从前端获取手机号, 验证码
     *    2. 从redis获取验证码
     *    3. 进行比对校验, 若对比失败,返回给前端(验证码输入错误)
     * 2. 调用Service完成订单保存
     * 3. 预约成功短信通知预约人
     *
     * @param map 提交预约页面 传递过来的数据
     * @return
     */
    @RequestMapping("/submitOrder")
    public Result submitOrder(@RequestBody Map<String,String> map){
        ZookeeperLock zookeeperLock = new ZookeeperLock();
        Lock lock = zookeeperLock.lock("test", 2 * 60 * 1000);
        try {
            //控制台 打印 页面传过来的数据
            System.out.println("map = " + map);
            //map = {setmealId=12, idCard=123456789012345678, sex=1, name=晓明, validateCode=21b8, telephone=18500022116, orderDate=2020-04-16}
            //验证短信验证码
            //1.从前端获取手机号, 验证码
            String telephone = map.get("telephone");
            String validateCode = map.get("validateCode");
            //2. 从redis获取验证码
            String code4Redis = jedisPool.getResource().get(telephone + RedisConst.SENDTYPE_ORDER);
            System.out.println("telephone="+telephone +", validateCode="+validateCode +", code4Redis="+code4Redis);
            //3. 进行比对校验, 若对比失败,返回给前端(验证码输入错误)
            if(code4Redis ==null || !code4Redis.equals(validateCode) ){
                return new Result(false, MessageConst.VALIDATECODE_ERROR);
            }

            //调用Service完成订单保存
            Result result = orderService.addOrder(map);
            /*
            //预约成功短信通知预约人
            if(result.getFlag()){
                //获取预约的日期, 发短信给预约人
                String orderDate = map.get("orderDate");
                SMSUtils.sendShortMessageNotice(telephone, orderDate);
            }
            */
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "提交预约失败");
        } finally {
            zookeeperLock.unlock(lock);
        }
    }
}
